import { Controller, Get, Post,UploadedFile,UseInterceptors, Header} from '@nestjs/common';
import { ApiBody, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AppService } from './app.service';
import { FileInterceptor } from '@nestjs/platform-express';
import multer = require('multer');

export const ApiFile =
  (fileName: string = 'file'): MethodDecorator =>
  (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    ApiBody({
      schema: {
        type: 'object',
        properties: {
          [fileName]: {
            type: 'string',
            format: 'binary',
          },
        },
      },                                                            
    })(target, propertyKey, descriptor);
  };
// 主路径为 app
@ApiTags("公共接口")
@Controller('app') // @Controller 定义控制器  传入一个路径参数作为访问这个控制器的主路径
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get() // 1.请求方法 可以传入一个路径参数作为路由的此路径，以访问这个方法
  getHello() {
    return this.appService.getHello();
  }

  // 2.通配符路径(?+* 三种通配符 )
  // 可以匹配到 get请求, http://localhost:9080/app/user_xxx
  // @Get("user_*")
  // getUser(){return "getUser"}
  
  // 3.带参数路径
  // 可以匹配到put请求，http://localhost:9080/app/list/xxxx
  // @Put("list/:id")
  // update(){ return "update"}

 
  @Post('upload')
  @ApiOperation({ summary: '上传文件' })
  @ApiConsumes('multipart/form-data')
  @ApiFile()
  @UseInterceptors(
    FileInterceptor('file', {
      storage: multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, "C:/Users/Administrator/Desktop/图片/");
        },
        filename: (req, file, cb) => {
          cb(null, file.originalname);
        },
      }),
    })
  )
  async uploadFile(@UploadedFile('file') file: any): Promise<any> {
      return await this.appService.uploadFile(file);
  }
}

